package com.gwsoft.mm.control;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import com.gwsoft.mm.dao.MonitorFilter;
import com.gwsoft.mm.service.MonitorFilterService;


@Controller
public class MonitorFilterControl {
	
	private static Logger log = Logger.getLogger(MonitorFilterControl.class);
	
	@Resource(name="mfservice")
	private MonitorFilterService mfService;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/{method}")
	public ModelAndView doAction(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@PathVariable String method){		
		Map<String, Object> map = new HashMap<String, Object>();
		List<MonitorFilter> mfList = new ArrayList<MonitorFilter>();
		
		Method fMethod;
		ModelAndView model;
		
		if(null !=request.getParameter("id")){
			map.put("id", request.getParameter("id"));
		}
		if(null !=request.getParameter("filter_id")){
			map.put("filter_id", request.getParameter("filter_id"));
		}
		if(null !=request.getParameter("monitor_id")){
			map.put("monitor_id", request.getParameter("monitor_id"));
		}
		if(null !=request.getParameter("compare_rate")){
			map.put("compare_rate", request.getParameter("compare_rate"));
		}
		if(null !=request.getParameter("start_date")){
			map.put("start_date", request.getParameter("start_date"));
		}
		if(null !=request.getParameter("execute_time")){
			map.put("execute_time", request.getParameter("execute_time"));
		}
		if(null !=request.getParameter("compare_type")){
			map.put("compare_type", request.getParameter("compare_type"));
		}
		if(null !=request.getParameter("is_use")){
			map.put("is_use", request.getParameter("is_use"));
		}
		if(null !=request.getParameter("filter_priority")){
			map.put("filter_priority", request.getParameter("filter_priority"));
		}
		if(null !=request.getParameter("filter_name")){
			map.put("filter_name", request.getParameter("filter_name"));
		}
		if(null !=request.getParameter("monitor_name")){
			map.put("monitor_name", request.getParameter("monitor_name"));
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		model = new ModelAndView("showFilterList");
		try{
			fMethod = mfService.getClass().getMethod(method, map.getClass());
			resultMap = (Map<String, Object>) fMethod.invoke(mfService, map);
		}catch(Exception e){
			model.addObject("error", "调用方法:"+method+"失败");
			e.printStackTrace();
		}
		model.addObject("mfList", mfList);
		return model;
	}
}
